﻿<template>
  <div>
    <query-table ref="query_table" :columns="columns" :query-fields="queryFields" :detail-fields="infoFields" :append-buttons="appendButtons" base-url="/user" :beforeDisplayDetail="afterLoadUserDetail" show-index></query-table>
    <el-dialog
        title="初始化密码"
        :visible.sync="initPswDlgVisible"
        append-to-body
        :close-on-click-modal="false"
        width="50%">
      <custom-field-form :fields="initPswFields" :data="user" columns="2" ></custom-field-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success"  @click="initPassword">保存</el-button>
        <el-button  @click="initPswDlgVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  $$route: {
    meta: {
      //标签页标题,如果不设置默认按菜单名称显示
      title: '用户管理'
    }
  },
  name: "UserList",
  data() {
    return {
      initPswDlgVisible:false,

      //初始化密码对话框字段
      initPswFields:[{
        id:'loginName',
        name:'登录名',
        show:true,
        disabled:true
      },{
        id:'name',
        name:'姓名',
        disabled: true
      },{
        id:'password',
        name:'密码',
        component:'password',
      },],
      //展示列信息
      columns: [
        {id: 'loginName', name: '登录名', width: 400},
        {id: 'name', name: '姓名',width: 150},
        {id: 'roleList', name: '角色', component: 'tag', type: 'name-display', nameKey: 'name'},
        // {id: 'companyId', name: '所属公司', type: 'office',width:250},
        {id: 'officeId', name: '所属单位', type: 'office',width:250},
        {
          id: 'operate', name: '操作', type: 'button', children: [
            {text: '初始化密码', permission: 'sys_user:create', click:(row)=>{
              this.user = row;
              this.initPswDlgVisible=true;}},
            {text: '编辑', permission: 'sys_user:update', operation: 'edit'},
            {text: '删除', type: 'danger', permission: 'sys_user:delete', operation: 'delete'},
          ]
        },
      ],
      //查询条件区域附加按钮
      appendButtons: [
        {
          text: '新增', type: 'success', operation:'create', click: () => {
            this.userDlgVisible = true;
            this.user = {};
          }
        },
        {
          text: '同步用户', type: 'success',  click: this.syncUser,isHide:()=>{
            return !this.checkPermission('sys_user:sync')
          }
        }
      ],
      //查询条件字段
      queryFields: [
        {id: 'loginName', name: '登录名', colSpan:5},
        {id: 'name', name: '姓名', colSpan: 4},
       // {id: 'companyId', name: '所属公司', colSpan: 6, component: 'company-select'},
        {
          id: 'officeId',
          //parent: 'companyId',
          name: '所属单位',
          colSpan: 6,
          component: {id:'company-select',showOffice:true}
        },
        {id: 'roleId', name: '用户角色', colSpan: 4, component: 'data-select', dataUrl: '/role/data'},
      ],
      infoFields:[{
        id:'loginName',
        name:'登录名',

        show:true,
        disableOnEdit:true //编辑时禁用
      },{
        id:'password',
        name:'密码',
        component:'password',
        hideOnEdit:true, //编辑时隐藏
      },{
        id:'name',
        name:'姓名',
      },{
        id:'officeId',
        name:'所属单位',
        component: {id:'company-select',showOffice:true}
      },
      //   {
      //   id:'companyId',
      //   name:'所属公司',
      //   component: 'company-select'
      // },{
      //   id:'officeId',
      //   name:'所属部门',
      //   parent:'companyId',
      //   component: 'data-select',
      //   dataUrl:'/company/data?parentId=${parent}&leafOfficeType=true',
      // },
        {
        id:'roleIdList',
        name:'所属角色',
        component: 'data-select',
        dataUrl: '/role/data',
        multiple:true,
        dataType:'array'
      },],
      companyMap: {},
      //编辑用户对话框可视状态
      userDlgVisible: false,
      //当前查看/编辑用户数据
      user: {},
      //加载状态
      loadingUserInfo: false,
    }
  },
  computed: {},
  created: function () {
  },
  watch: {
    '$route'(to, from) {
    },
  },
  methods: {
    syncUser() {
      const  loading = this.openLoading();
      this.$post("/sso/sync/user",{},(res)=>{
        this.$message.success("同步完成")
      },()=>{
        this.$refs['query_table'].reload();
        loading.close();
      })
    },

    //用户数据加载完成处理
    afterLoadUserDetail(detailData) {
      var roleIdList = [];
      if (detailData.roleList) {
        for (var item of detailData.roleList) {
          roleIdList.push(item.id);
        }
      }
      detailData.roleIdList = roleIdList;
    },
    //初始化用户密码
    initPassword() {
      if(this.user.password.length<=0) {
        this.$message.warning("密码无效");
        return;
      }
      let user = JSON.parse(JSON.stringify(this.user));
      const loading = this.openLoading("正在处理...")
      let rsaPassword = this.$encryption(user.password);
      user.password = rsaPassword;
      this.$post('/user/init_password',user,(response)=>{
        this.$message.success("设置成功")
        this.initPswDlgVisible = false;
      },()=>{
        loading.close();
      })
    }

  },

}
</script>
